package com.efast.dmom.mgmt.service.mes.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.mes.CertificateNumberBean;
import com.efast.dmom.mgmt.dao.mes.CertificateNumberDao;
import com.efast.dmom.mgmt.dao.mes.PmFeaturValueDao;
import com.efast.dmom.mgmt.dao.mes.PmPartModelAndVinRuleDao;
import com.efast.dmom.mgmt.entity.mes.PmFeaturValue;
import com.efast.dmom.mgmt.entity.mes.PmPartModelAndVinRule;
import com.efast.dmom.mgmt.entity.mes.TwBasLot;
import com.efast.dmom.mgmt.service.mes.CertificateNumberService;

@Service
public class CertificateNumberServiceImpl implements CertificateNumberService{

	@Autowired
	private CertificateNumberDao certificateNumberDao;

	@Autowired
	private PmFeaturValueDao pmFeaturValueDao;
	@Autowired
	private PmPartModelAndVinRuleDao vinRuleDao;
	
	@Override
	public ResultBean saveCertificateNumber(TwBasLot bean) {
		ResultBean result = new ResultBean(true, "'");
        if (bean != null) {
          if(bean.getVin() != null && !"".equals(bean.getVin())) {
        	CertificateNumberBean certificate =new CertificateNumberBean();
        	certificate.setLotId(bean.getLotId());
        	certificate.setVin(bean.getVin());
        	String partNoVersion = bean.getPartNoVersion();
        	String paintColorNum = partNoVersion.substring(partNoVersion.length() - 9, partNoVersion.length() - 8);
    		String colorNum = partNoVersion.substring(partNoVersion.length() - 8, partNoVersion.length() - 6);
    		
    		//获取车型
    		List<PmPartModelAndVinRule> listVinRuleInfo = vinRuleDao.getVinRuleInfo(partNoVersion);
    		if (listVinRuleInfo != null && listVinRuleInfo.size() > 0) {
    			String model = listVinRuleInfo.get(0).getModel();
    			certificate.setCarModel(model);
    		}
    		//获取颜色
    		// 获取车漆配置信息
    		List<PmFeaturValue> paintColorNumList = pmFeaturValueDao.getFeaturValueByType("vehiclePaintType",
    				paintColorNum);
    		// 获取颜色配置信息
    		List<PmFeaturValue> colorNumList = pmFeaturValueDao.getFeaturValueByType("vehicleColor", colorNum);
    		if(paintColorNumList != null && paintColorNumList.size() >0 && colorNumList != null && colorNumList.size() > 0) {
    			String paintColor = paintColorNumList.get(0).getFeatueValueDesc();
    			String color = colorNumList.get(0).getFeatueValueDesc();
    			certificate.setColor(paintColor+"-"+color);
    		}
    		//获取电机号和电机型号
    		CertificateNumberBean data = certificateNumberDao.selectvalueByVin(bean.getVin());
    		if(data != null) {
    			certificate.setMachineryNo(data.getMachineryNo());
    			certificate.setMachineryType(data.getMachineryType());
    		}
    		certificate.setCreateTime(new Date());
    		certificate.setCreateUser(SpringWebUtils.getRequestUser());
    		certificate.setCompanyCode(SpringWebUtils.getRequestCompany());
    		certificateNumberDao.saveCertificateNumber(certificate);
    		result.setSuccess(true);
    		result.setMessage("数据存储成功！");
          } else {
        	  result.setSuccess(false);
        	  result.setMessage("获取的lot信息中vin号为空！");
          }
        } else {
        	result.setSuccess(false);
        	result.setMessage("获取的lot信息为空！");
        }
		return result;
	}
}
